<?php
use com\gdl\common\dataobject\common\ResultDO; 
use com\gdl\newagk\interfaces\AdminCompanyServiceIf;
use com\gdl\newagk\dataobject\admincompany\CompanyDTO; 
require(dirname(dirname(__FILE__)) ."/php_sdk-master/include.php");
use MagicCrypt\MagicCrypt;
/**
 * Admin后台公司相关接口 
 * 此接口包含后台公司 部门 分组 专业
 */
class api_AdminCompanyService extends api_Abstract implements AdminCompanyServiceIf{

    /**
     * admin公司列表接口
     */
    public function companyList(CompanyDTO $company)
    {
        $where = $company->where ? $company->where : "";
		$offset = $company->offset ? gdl_lib_BaseUtils::getStr($company->offset, 'int') : 0;
        $num = $company->num ? gdl_lib_BaseUtils::getStr($company->num, 'int') : 10;
        $page = $offset * $num;
		$result = new ResultDO();
        $result->success = true;
		$result->message = "暂无数据";
        $result->code = 400; 
        $result->data = []; 
        try{
			$companyModel = new model_newagk_company();
			$platformModel = new model_newagk_platform();
			if($offset=='0') $offset=1;
			$rows = $num;
			$limit = ($offset - 1) * $rows . "," . $rows;
			$group = 'order by id desc limit '.$limit;
            $res = $companyModel->select($where,"*",$group)->items;
			$resCount = $companyModel->select($where,"count(id) as num")->items;
			# 查询出所有平台
			$platformList = $platformModel->select("isdelete=0","*")->items;
			//var_dump($platformList);exit;
			for($i=0; $i<count($res);$i++){
				for($j=0; $j<count($platformList); $j++){
					if($res[$i]['admin_reg']==$platformList[$j]['admin_reg']){
						$res[$i]['platformname'] = $platformList[$j]['reals']; 
					}
				}
			}
			
			if($res){ 
				$result->data = $res;
				$result->code = 200;
				$result->message = $resCount[0]['num'];
			}
        } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage(); 
        }
        return $result; 
    }
     
    /**
     *  修改/添加公司
     */
    public function editCompany(CompanyDTO $company){
		$platformId = $company->id ? gdl_lib_BaseUtils::getStr($company->id, 'int') : 0;
		$data = $company->data ? gdl_lib_BaseUtils::getStr($company->data) : [];
		$companyId = $data['id']?$data['id']:0;
		$result = new ResultDO();
		$result->success = true;
		$result->code = 400;
		$result->message = "操作失败";
		$result->data = [];
		try{
			$companyModel = new model_newagk_company();
			$platformModel = new model_newagk_platform();
			$adminUserModel = new model_newagk_adminuser();
			if($platformId==0 || empty($platformId)){
				$admin_reg = $adminUserModel->selectOne("id=1","reg admin_reg");
			}else{
				$admin_reg = $platformModel->selectOne("id='{$platformId}'","admin_reg");
			}
			$data['admin_reg'] = isset($admin_reg['admin_reg'])?$admin_reg['admin_reg']: "";
			# 添加
			if($companyId ==0 || empty($companyId)){ 
				$insertData = [];
				foreach($data as $key=>$v){
					if($key!='id' && $key!='updateTime' ){
						$insertData[$key] =$v;
					}
				}
				$ret = $companyModel->insert($insertData); 
			}else{
			# 修改
				$upData = [];
				foreach($data as $key=>$v){
					if($key!='id' && $key!='createTime' ){
						$upData[$key] =$v;
					}
				}
				$ret = $companyModel->update("id='{$companyId}'",$upData);
			}
			if($ret){
				$result->message = "操作成功";
				$result->code = 200;
			} 
		}catch(Exception $e){	
			$result->success = false;
			$result->code = 500;
			$result->message =  $e->getMessage();
		}
		return $result;
    }
    
    /**
     *  部门列表
     */
    public function departmentList(CompanyDTO $company){
        $where = $company->where ? $company->where : "";
		$companyId = $company->id ? $company->id : 0;
		$offset = $company->offset ? gdl_lib_BaseUtils::getStr($company->offset, 'int') : 0;
        $num = $company->num ? gdl_lib_BaseUtils::getStr($company->num, 'int') : 0;
        $page = $offset * $num;
		$result = new ResultDO();
        $result->success = true;
		$result->message = "暂无数据";
        $result->code = 400; 
        $result->data = []; 
        try{
			$companyModel = new model_newagk_company();
			$departmentModel = new model_newagk_department();
			$platformModel = new model_newagk_platform();
			if($offset=='0') $offset=1;
			$rows = $num;
			$limit = ($offset - 1) * $rows . "," . $rows;
			$group = 'order by id desc limit '.$limit;
            $res = $departmentModel->select($where,"*",$group)->items;
			$resCount = $departmentModel->select($where,"count(id) as num")->items;
			# 查出所有公司
			$companyList = $companyModel->select("isdelete=0", "*")->items;
			
			# 查询出所有平台
			$platformList = $platformModel->select("isdelete=0","*")->items; 
			for($i=0; $i<count($res);$i++){
				$res[$i]['platformname'] = "";
				$res[$i]['companyname'] = "";
				for($j=0; $j<count($platformList); $j++){
					if($res[$i]['admin_reg']==$platformList[$j]['admin_reg']){
						$res[$i]['platformname'] = $platformList[$j]['real'];
					}
				}
				if($companyList){
					for($k=0; $k<count($companyList); $k++){
						if($res[$i]['companyId']==$companyList[$k]['id']){
							$res[$i]['companyname'] = $companyList[$k]['name'];
						}
					}
				}
			}
			
			if($res){ 
				$result->data = $res;
				$result->code = 200;
				$result->message = $resCount[0]['num'];
			}
			
            
			
        } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage(); 
        }
        return $result; 
    }
    /**
     *  修改部门/添加
     */
    public function editDepartment(CompanyDTO $company){
        $platformId = $company->id ? gdl_lib_BaseUtils::getStr($company->id, 'int') : 0;
		$data = $company->data ? gdl_lib_BaseUtils::getStr($company->data) : [];
		$departmentId = $data['id']?$data['id']:0;
		$result = new ResultDO();
		$result->success = true;
		$result->code = 400;
		$result->message = "操作失败";
		$result->data = [];
		try{
			$departmentModel = new model_newagk_department();
			$platformModel = new model_newagk_platform();
			$adminUserModel = new model_newagk_adminuser();
			if($platformId==0 || empty($platformId)){
				$admin_reg = $adminUserModel->selectOne("id=1","reg admin_reg");
			}else{
				$admin_reg = $platformModel->selectOne("id='{$platformId}'","admin_reg");
			}
			$data['admin_reg'] = isset($admin_reg['admin_reg'])?$admin_reg['admin_reg']: "";
			# 添加
			if($departmentId ==0 || empty($departmentId)){ 
				$insertData = [];
				foreach($data as $key=>$v){
					if($key!='id' && $key!='updateTime' ){
						$insertData[$key] =$v;
					}
				}
				$ret = $departmentModel->insert($insertData);
				
			}else{
			# 修改
				$upData = [];
				foreach($data as $key=>$v){
					if($key!='id' && $key!='createTime' ){
						$upData[$key] =$v;
					}
				}
				$ret = $departmentModel->update("id='{$departmentId}'",$upData);
			}
			if($ret){
				$result->message = "操作成功";
				$result->code = 200;
			} 
		}catch(Exception $e){	
			$result->success = false;
			$result->code = 500;
			$result->message =  $e->getMessage();
		}
		return $result;
    } 

	/**
     *  班组列表
     */
    public function groupList(CompanyDTO $company){
       $where = $company->where ? $company->where : "";
		$companyId = $company->id ? $company->id : 0;
		$offset = $company->offset ? gdl_lib_BaseUtils::getStr($company->offset, 'int') : 0;
        $num = $company->num ? gdl_lib_BaseUtils::getStr($company->num, 'int') : 0;
        $page = $offset * $num;
		$result = new ResultDO();
        $result->success = true;
		$result->message = "暂无数据";
        $result->code = 400; 
        $result->data = []; 
        try{
			$companyModel = new model_newagk_company();
			$departmentModel = new model_newagk_department();
			$platformModel = new model_newagk_platform();
			if($offset=='0') $offset=1;
			$rows = $num;
			$limit = ($offset - 1) * $rows . "," . $rows;
			$group = 'order by id desc limit '.$limit;
            $res = $departmentModel->select($where,"*",$group)->items;
			$resCount = $departmentModel->select($where,"count(id) as num")->items;
			# 查出所有公司
			$companyList = $companyModel->select("isdelete=0", "*")->items;
			
			# 查出所有部门
			$departmentList = $departmentModel->select('pid=0')->items;
			# 查询出所有平台
			$platformList = $platformModel->select("isdelete=0","*")->items; 
			for($i=0; $i<count($res);$i++){
				$res[$i]['platformname'] = "";
				$res[$i]['companyname'] = "";
				$res[$i]['departmentname'] = "";
				for($j=0; $j<count($platformList); $j++){
					if($res[$i]['admin_reg']==$platformList[$j]['admin_reg']){
						$res[$i]['platformname'] = $platformList[$j]['reals'];
					}
				}
				# 公司名
				if($companyList){
					for($k=0; $k<count($companyList); $k++){
						if($res[$i]['companyId']==$companyList[$k]['id']){
							$res[$i]['companyname'] = $companyList[$k]['name'];
						}
					}
				}
				# 部门名
				if($departmentList){
					for($y=0; $y<count($departmentList); $y++){
						if($res[$i]['id']==$departmentList[$y]['pid']){
							$res[$i]['departmentname'] = $departmentList[$y]['name'];
						}
					}
				}
			}
			
			if($res){ 
				$result->data = $res;
				$result->code = 200;
				$result->message = $resCount[0]['num'];
			}
			
            
			
        } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage(); 
        }
        return $result; 
    }
    /**
     *  修改班组/添加
     */
    public function editGroup(CompanyDTO $company){
        $platformId = $company->id ? gdl_lib_BaseUtils::getStr($company->id, 'int') : 0;
		$data = $company->data ? gdl_lib_BaseUtils::getStr($company->data) : [];
		$groupId = $data['id']?$data['id']:0;
		$result = new ResultDO();
		$result->success = true;
		$result->code = 400;
		$result->message = "操作失败";
		$result->data = [];
		try{
			$departmentModel = new model_newagk_department();
			$platformModel = new model_newagk_platform();
			$adminUserModel = new model_newagk_adminuser();
			if($platformId==0 || empty($platformId)){
				$admin_reg = $adminUserModel->selectOne("id=1","reg admin_reg");
			}else{
				$admin_reg = $platformModel->selectOne("id='{$platformId}'","admin_reg");
			}
			$data['admin_reg'] = isset($admin_reg['admin_reg'])?$admin_reg['admin_reg']: "";
			# 添加
			if($groupId ==0 || empty($groupId)){ 
				$insertData = [];
				foreach($data as $key=>$v){
					if($key!='id' && $key!='updateTime' ){
						$insertData[$key] =$v;
					}
				}
				if($data['companyId']==0 || empty($data['companyId'])){
					$companyId = $departmentModel->selectOne("id='{$data['pid']}'","companyId");
					$insertData['companyId'] = $companyId['companyId'];
				}
				$ret = $departmentModel->insert($insertData);
				
			}else{
			# 修改
				$upData = [];
				foreach($data as $key=>$v){
					if($key!='id' && $key!='createTime' ){
						$upData[$key] =$v;
					}
				}
				if($data['companyId']==0 || empty($data['companyId'])){
					$companyId = $departmentModel->selectOne("id='{$data['pid']}'","companyId");
					
					$upData['companyId'] = $companyId['companyId'];
				}
				$ret = $departmentModel->update("id='{$groupId}'",$upData);
			}
			if($ret){
				$result->message = "操作成功";
				$result->code = 200;
			} 
		}catch(Exception $e){	
			$result->success = false;
			$result->code = 500;
			$result->message =  $e->getMessage();
		}
		return $result;
    } 
	
	/**
     *  专业列表
     */
    public function specialtyList(CompanyDTO $company){
        $where = $company->where ? $company->where : "";
        $platformId = $company->id ? $company->id : 0;
        $offset = $company->offset ? gdl_lib_BaseUtils::getStr($company->offset, 'int') : 0;
        $num = $company->num ? gdl_lib_BaseUtils::getStr($company->num, 'int') : 0;
        $page = $offset * $num;
	$result = new ResultDO();
        $result->success = true;
	$result->message = "暂无数据";
        $result->code = 400; 
        $result->data = []; 
        try{
			$specModel = new model_newagk_specialty(); 
			$platformModel = new model_newagk_platform();
			$companyModel = new model_newagk_company();
			if($offset=='0') $offset=1;
			$rows = $num;
			$limit = ($offset - 1) * $rows . "," . $rows;
			$group = 'order by id desc limit '.$limit;
                        $res = $specModel->select($where,"*",$group)->items;  
						$resCount = $specModel->select($where,"count(id) as num")->items;
                        # 查出所有公司
			$companyList = $companyModel->select("isdelete=0", "*")->items;
			# 查询出所有平台
			$platformList = $platformModel->select("isdelete=0","*")->items; 
			for($i=0; $i<count($res);$i++){
				$res[$i]['platformname'] = "";
				$res[$i]['companyname'] = "";
				$res[$i]['departmentname'] = "";
				for($j=0; $j<count($platformList); $j++){
					if($res[$i]['admin_reg']==$platformList[$j]['admin_reg']){
						$res[$i]['platformname'] = $platformList[$j]['real'];
					}
				} 
                                if($companyList){
					for($k=0; $k<count($companyList); $k++){
						if($res[$i]['companyId']==$companyList[$k]['id']){
							$res[$i]['companyname'] = $companyList[$k]['name'];
						}
					}
				}
			}
			
			if($res){ 
				$result->data = $res;
				$result->code = 200;
				$result->message = $resCount[0]['num'];
			}
			
            
			
        } catch (Exception $e) {
                $result->success = false;
                $result->code = $e->getCode();
                $result->message = $e->getMessage(); 
        }
        return $result;
    }
    /**
     *  修改专业/添加
     */
    public function editSpecialty(CompanyDTO $company){
        $platformId = $company->id ? gdl_lib_BaseUtils::getStr($company->id, 'int') : 0;
		$data = $company->data ? gdl_lib_BaseUtils::getStr($company->data) : [];
		$specId = $data['id']?$data['id']:0;
		$result = new ResultDO();
		$result->success = true;
		$result->code = 400;
		$result->message = "操作失败";
		$result->data = [];
		try{
			$specModel = new model_newagk_specialty();
			$platformModel = new model_newagk_platform();
			$adminUserModel = new model_newagk_adminuser();
			if($platformId==0 || empty($platformId)){
				$admin_reg = $adminUserModel->selectOne("id=1","reg admin_reg");
			}else{
				$admin_reg = $platformModel->selectOne("id='{$platformId}'","admin_reg");
			}
			$data['admin_reg'] = isset($admin_reg['admin_reg'])?$admin_reg['admin_reg']: "";
			# 添加
			if($specId ==0 || empty($specId)){ 
				$insertData = [];
				foreach($data as $key=>$v){
					if($key!='id' && $key!='updateTime' ){
						$insertData[$key] =$v;
					}
				}
				$ret = $specModel->insert($insertData);
				
			}else{
			# 修改
				$upData = [];
				foreach($data as $key=>$v){
					if($key!='id' && $key!='createTime' ){
						$upData[$key] =$v;
					}
				}
				$ret = $specModel->update("id='{$specId}'",$upData);
			}
			if($ret){
				$result->message = "操作成功";
				$result->code = 200;
			} 
		}catch(Exception $e){	
			$result->success = false;
			$result->code = 500;
			$result->message =  $e->getMessage();
		}
		return $result;
    } 
}
